//
//  GQHTruckView.m
//  Xanthium
//
//  Created by GuanQinghao on 01/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHTruckView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@interface GQHTruckView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *titleArray;
}

@end


@implementation GQHTruckView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"所有人",@"所有人证件号",@"车牌号",@"行驶证",@"道路运输许可证",@"道路经营许可证号",@"道路运输许可证号",@"挂车牌照号",@"车型",@"车长",@"载重",@"车辆照片"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (3 == indexPath.row || 4 == indexPath.row || 11 == indexPath.row) {
        
        return 60.0f;
    }
    
    if (1 == indexPath.row) {
        
        return 0.0f;
    }
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (3 == indexPath.row || 4 == indexPath.row || 11 == indexPath.row) {
        
        static NSString *identifier = @"aCell";
        GQHTruckImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHTruckImagesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.qh_captionLabel.text = titleArray[indexPath.row];
        
        if (11 == indexPath.row) {
            
            NSString *imagesString = _qh_contentArray[indexPath.row];
            NSArray<NSString *> *imagesArray = [imagesString componentsSeparatedByString:@","];
            
            NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:imagesArray[0]];
            NSURL *URL = [NSURL URLWithString:URLString];
            [cell.qh_rightImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
            cell.qh_rightImageView.userInteractionEnabled = YES;
            cell.qh_rightImageView.tag = 11;
            
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewImageAction:)];
            [cell.qh_rightImageView addGestureRecognizer:tap];
            
            NSString *URLString1 = [[NSString qh_imageURLString] stringByAppendingPathComponent:imagesArray[1]];
            NSURL *URL1 = [NSURL URLWithString:URLString1];
            [cell.qh_leftImageView sd_setImageWithURL:URL1 placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
            cell.qh_leftImageView.userInteractionEnabled = YES;
            cell.qh_leftImageView.tag = 12;
            
            UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewImageAction:)];
            [cell.qh_leftImageView addGestureRecognizer:tap1];
            
        } else {
            
            NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_contentArray[indexPath.row]];
            NSURL *URL = [NSURL URLWithString:URLString];
            [cell.qh_rightImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
            cell.qh_leftImageView.userInteractionEnabled = YES;
            cell.qh_leftImageView.image = nil;
            cell.qh_rightImageView.userInteractionEnabled = YES;
            cell.qh_rightImageView.tag = indexPath.row;
            
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewImageAction:)];
            [cell.qh_rightImageView addGestureRecognizer:tap];
        }
        
        return cell;
    } else {
        
        static NSString *identifier = @"bCell";
        GQHCustomTextDoubleHorizontalCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHCustomTextDoubleHorizontalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.userInteractionEnabled = NO;
            cell.hidden = NO;
        }
        
        if (1 == indexPath.row) {
            
            cell.hidden = YES;
        }
        
        cell.qh_leftLabel.text = titleArray[indexPath.row];
        cell.qh_rightLabel.text = _qh_contentArray[indexPath.row];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)viewImageAction:(UITapGestureRecognizer *)tap
{
    NSInteger tag = tap.view.tag;
    
    NSString * urlString = nil;
    if (tag == 11)
    {
        NSArray * array = [_qh_contentArray[tag] componentsSeparatedByString:@","];
        urlString = [NSString stringWithFormat:@"%@%@",[NSString qh_imageURLString],array[0]];
    }
    else if (tag == 12)
    {
        NSArray * array = [_qh_contentArray[11] componentsSeparatedByString:@","];
        urlString = [NSString stringWithFormat:@"%@%@",[NSString qh_imageURLString],array[1]];
    }
    else
    {
        urlString = [NSString stringWithFormat:@"%@%@",[NSString qh_imageURLString],_qh_contentArray[tag]];
    }
    CGRect rect1 = [tap.view convertRect:tap.view.frame fromView:tap.view.superview];//获取imagevuew在subview的位置
    CGRect rect2 = [tap.view convertRect:rect1 toView:self];
    
    YHPhotoBrowser *photoView = [[YHPhotoBrowser alloc]init];
    photoView.sourceView = self;//图片所在的父容器
    photoView.urlImgArr = @[urlString]; //网络链接图片的数组
    photoView.sourceRect = rect2; // 图片的frame
    photoView.indexTag = 0;   //初始化进去显示的图片下标
    [photoView show];
}

@end
